Ext.define('DCApp.view.PageList', {
    extend: 'Ext.grid.Panel',
    alias: 'widget.pagelist',
    
    requires: [
        'DCApp.controller.ListController',
        'DCApp.ux.GridFilters',
        'Ext.overrides.grid.filters.filter.Date'
    ],

    controller: 'list',
    plugins: ['gfilters'],
    bind: {
        store: '{objs}'
    },
    header:false,

    selModel: {
        selType: 'checkboxmodel',
        showHeaderCheckbox: false
    },
    tbar: ['->',{
        text: '录入',
        handler: 'onNew',
        hidden:true        	
    },{
        text: '克隆',
        handler: 'onClone',
        hidden:true
    },{
        text: '删除',
        handler: 'onRemove',
        hidden:true
    }
    ],
    bbar: [{
    	xtype: 'pagingtoolbar',
        bind: {
            store: '{objs}'
        },
        displayInfo: true,
        displayMsg: '当前: {0} - {1} 总数: {2}',
        emptyMsg: "无",
        items:['-']
    }],
   
	
	listeners: {
        itemdblclick: 'onDblClick',
    	afterlayout: function(grid,layout, eOpts) {
    		var bbar = grid.down('pagingtoolbar');
    		var st =bbar.getStore();
    		if(!st || st.isEmptyStore)
    			return;
    		var maxRowsPerGrid = Math.floor(grid.getView().getHeight()/DCApp.Cfg.ROW_HEIGHT)-1;
    		if(st.getPageSize()!=maxRowsPerGrid){
        		st.setPageSize(maxRowsPerGrid);
        		bbar.doRefresh();
    		}
    	}
    },
    
    initComponent: function(){
    	var me=this;
    	me.callParent();
    	if(DCApp.Cfg.bView){
    		var detailPanel = me.up('detail');
    		if(detailPanel){  
    			var tbar = me.down('toolbar');
    			tbar.hide();
    		}    		
    	}else{
        	var perms = DCApp.Cfg.usrInfo.get('perms')||'update;delete';
        	//只读显示
        	if(perms.indexOf('update')!=-1){
                if (me.down('button[text="录入"]')) {
                    me.down('button[text="录入"]').setVisible(true);
                }    		
                if (me.down('button[text="克隆"]')) {
                    me.down('button[text="克隆"]').setVisible(true);
                }    		
        	}
        	if(perms.indexOf('delete')!=-1){ //管理员才显示删除按钮
                if (me.down('button[text="删除"]')) {
                    me.down('button[text="删除"]').setVisible(true);
                }
        	}  	
            me.on('edit', function(editor, e) {
                // commit the changes right after editing finished
        		var rec = e.record;
        		//rec.commit(true);
        		rec.save({
                    scope: this,
                    callback: function(records,operation,success){
                    	if(success)
                    	 Ext.toast({
                             title: '保存',
                             html: '['+rec.getId()+']保存完成',
                             align: 't',
                             bodyPadding: 10
                         });
                    }
                });
                
            });
    		
    	}
    }
 });
